package practise;

public class ChkPalindrome {
    public boolean chkPalindrome(ListNode A) {
        if(A == null) {
            return false;
        }
        // 确定中间节点
        ListNode slow = A;
        ListNode fast = A;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode prev = slow;
        slow = slow.next;
        ListNode next = slow.next;
        // 对中间节点即以后的内容对调
        while(slow != null) {
            slow.next = prev;
            prev = slow;
            slow = next;
            if(slow != null) {
                next = slow.next;
            }
        }
        // 回文判断
        ListNode left = A;
        ListNode right = prev;
        while(left != right && left.next != right) {
            if(left.val != right.val) {
                break;
            }
            left = left.next;
            right = right.next;
        }
        return left == right || left.next == right;
    }
}
